package mysupertux.game;

/**
 * Thread that draws the game field and objects.
 */
public class GameThread extends Thread {
	/** GameField. */
	private GameField gameField = null;
	/** True if the rendering loop is ready. */
	private boolean moveReady = false;

	/**
	 * Creates a ThreadVisualization from the given game field.
	 * 
	 * @param gameField
	 *            game field
	 */
	public GameThread(GameField gameField) {
		this.gameField = gameField;
	}

	public void run() {
		while (!interrupted()) {
			synchronized (this) {
				moveReady = true;
				try {
					notifyAll();
					wait(1000);
				} catch (InterruptedException e) {
				}
				moveReady = false;

				gameField.doMove();
			}
		}
	}

	public synchronized void doMove() {
		notifyAll();
		moveReady = false;
	}

	public synchronized void waitUntilReady() {
		while (!moveReady) {
			synchronized (this) {
				try {
					wait(5);
				} catch (InterruptedException e) {
				}
			}
		}
	}
}
